﻿<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>教务管理系统 - 我的课程</title>
    <link href="${pageContext.request.contextPath}/resources/bootstrap5/css/bootstrap.min.css" rel="stylesheet">
    <!-- Bootstrap Icons - 本地版本 -->
    <link href="${pageContext.request.contextPath}/resources/bootstrap-icons/1.11.3/bootstrap-icons.css" rel="stylesheet">
    <!-- Bootstrap Icons - 备用CDN -->
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap-icons/1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
    <!-- 第二个备用CDN -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css">
    <style>
        body {
            font-family: "Microsoft YaHei", sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 0;
        }

        .header {
            background-color: #1976d2;
            color: white;
            padding: 10px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            position: fixed;
            width: 100%;
            top: 0;
            z-index: 1000;
        }

        .header-left {
            display: flex;
            align-items: center;
            gap: 20px;
        }

        .header h2 {
            margin: 0;
            font-size: 1.5rem;
            display: flex;
            align-items: center;
        }

        .header h2 img {
            height: 36px;
            margin-right: 10px;
        }

        .header .breadcrumb {
            margin: 0;
            background: transparent;
            color: white;
        }

        .header .breadcrumb a {
            color: rgba(255,255,255,0.8);
            text-decoration: none;
        }

        .header .breadcrumb a:hover {
            color: white;
        }

        .header .breadcrumb-item.active {
            color: white;
        }

        .header .breadcrumb-item+.breadcrumb-item::before {
            color: rgba(255,255,255,0.6);
        }

        /* 侧边栏样式 */
        .sidebar {
            position: fixed;
            top: 56px;
            left: 0;
            bottom: 0;
            width: 250px;
            background-color: white;
            box-shadow: 2px 0 5px rgba(0,0,0,0.1);
            overflow-y: auto;
            z-index: 100;
            padding-top: 20px;
        }

        .sidebar-menu {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .sidebar-menu > li {
            margin-bottom: 5px;
        }

        .sidebar-menu > li > a {
            display: flex;
            align-items: center;
            padding: 12px 20px;
            color: #333;
            text-decoration: none;
            transition: all 0.3s;
            border-left: 3px solid transparent;
        }

        .sidebar-menu > li > a:hover {
            background-color: #f0f0f0;
            border-left-color: #1976d2;
        }

        .sidebar-menu > li > a.active {
            background-color: #e3f2fd;
            border-left-color: #1976d2;
            color: #1976d2;
        }

        .sidebar-menu > li > a i {
            margin-right: 10px;
            width: 20px;
            text-align: center;
            font-size: 1.1rem;
        }

        .sidebar-submenu {
            list-style: none;
            padding-left: 53px;
            max-height: 0;
            overflow: hidden;
            transition: max-height 0.3s ease;
        }

        .sidebar-menu > li.open .sidebar-submenu {
            max-height: 500px;
        }

        .sidebar-submenu > li > a {
            display: block;
            padding: 8px 0;
            color: #666;
            text-decoration: none;
            transition: all 0.3s;
        }

        .sidebar-submenu > li > a:hover {
            color: #1976d2;
        }

        .sidebar-submenu > li > a.active {
            color: #1976d2;
            font-weight: 500;
        }

        /* 内容区样式 */
        .content {
            margin-left: 250px;
            padding: 76px 20px 20px;
        }

        /* 用户下拉菜单样式 */
        .header-user {
            display: flex;
            align-items: center;
            position: relative;
        }
        .header-user-avatar {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            margin-right: 8px;
            object-fit: cover;
            background: #fff;
            cursor: pointer;
            transition: transform 0.2s;
        }
        .header-user-avatar:hover {
            transform: scale(1.1);
        }
        .header-user-name {
            font-size: 1rem;
            font-weight: 500;
            cursor: pointer;
            font-family: 'Microsoft YaHei', Arial, sans-serif;
        }
        .user-dropdown {
            position: absolute;
            top: 100%;
            right: 0;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
            min-width: 160px;
            z-index: 1001;
            opacity: 0;
            visibility: hidden;
            transform: translateY(-10px);
            transition: all 0.3s ease;
            margin-top: 8px;
        }
        .user-dropdown.show {
            opacity: 1;
            visibility: visible;
            transform: translateY(0);
        }
        .user-dropdown::before {
            content: '';
            position: absolute;
            top: -6px;
            right: 20px;
            width: 12px;
            height: 12px;
            background: #fff;
            transform: rotate(45deg);
            box-shadow: -2px -2px 4px rgba(0,0,0,0.1);
        }
        .user-dropdown-item {
            display: flex;
            align-items: center;
            padding: 12px 16px;
            color: #333;
            text-decoration: none;
            transition: background 0.2s;
            border-bottom: 1px solid #f0f0f0;
            font-family: 'Microsoft YaHei', Arial, sans-serif;
        }
        .user-dropdown-item:last-child {
            border-bottom: none;
        }
        .user-dropdown-item:hover {
            background: #f8f9fa;
            color: #1976d2;
        }
        .user-dropdown-item i {
            margin-right: 8px;
            width: 16px;
            text-align: center;
        }

        .card {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            margin-bottom: 20px;
            overflow: hidden;
        }

        .card-header {
            padding: 15px 20px;
            background-color: white;
            border-bottom: 1px solid #eee;
            font-weight: 600;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .card-body {
            padding: 20px;
        }

        .search-bar {
            display: flex;
            align-items: center;
            gap: 12px;
            margin-bottom: 18px;
            flex-wrap: nowrap;
        }

        .search-bar input {
            border-radius: 8px;
            border: 1px solid #e3e3e3;
            padding: 6px 14px;
            font-size: 1rem;
            flex: 1;
            min-width: 180px;
        }

        .search-bar .btn {
            border-radius: 8px;
            font-size: 1rem;
            padding: 6px 18px;
            font-weight: 500;
            box-shadow: none;
            flex-shrink: 0;
        }

        .btn-primary {
            background: #1976d2;
            border: none;
        }

        .btn-primary:hover {
            background: #1565c0;
        }

        .btn-warning {
            background: #ffb300;
            color: #fff;
            border: none;
        }

        .btn-warning:hover {
            background: #ff9800;
            color: #fff;
        }

        .btn-success {
            background: #4caf50;
            border: none;
        }

        .btn-success:hover {
            background: #388e3c;
        }

        .btn-danger {
            background: #f44336;
            border: none;
        }

        .btn-danger:hover {
            background: #d32f2f;
        }

        .table {
            background: #fff;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        }

        .table thead th {
            background: #f8f9fa;
            color: #1976d2;
            font-weight: bold;
            text-align: center;
            border-bottom: 2px solid #e3e3e3;
        }

        .table tbody tr {
            transition: background 0.2s;
        }

        .table tbody tr:hover {
            background: #f1f8ff;
        }

        .table td, .table th {
            vertical-align: middle !important;
            text-align: center;
        }

        .pagination {
            justify-content: center;
            margin-top: 12px;
        }

        .pagination .page-item .page-link {
            border-radius: 6px !important;
            margin: 0 2px;
            color: #1976d2;
            border: none;
            background: #fff;
            font-weight: 500;
            transition: background 0.2s, color 0.2s;
        }

        .pagination .page-item.active .page-link {
            background: #1976d2;
            color: #fff;
        }

        .pagination .page-item .page-link:hover {
            background: #e3f2fd;
            color: #1565c0;
        }

        @media (max-width: 768px) {
            .sidebar {
                transform: translateX(-100%);
                transition: transform 0.3s;
            }

            .sidebar.active {
                transform: translateX(0);
            }

            .content {
                margin-left: 0;
            }

            .search-bar {
                flex-wrap: wrap;
            }

            .search-bar input {
                min-width: 100%;
                margin-bottom: 10px;
            }
        }
    </style>
</head>
<body>
<!-- 顶部导航栏 -->
<div class="header">
    <div class="header-left">
        <h2><img src="${pageContext.request.contextPath}/resources/images/logo.png" alt="Logo"> 教务管理系统</h2>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="${pageContext.request.contextPath}/teacher/home">教师首页</a></li>
                <li class="breadcrumb-item active">我的课程</li>
            </ol>
        </nav>
    </div>

    <!-- 用户名下拉菜单 -->
    <div class="header-user" id="userDropdownContainer">
        <img src="${pageContext.request.contextPath}/resources/images/baimao.png" class="header-user-avatar" alt="用户名头像" id="userAvatar">
        <span class="header-user-name" id="userName">
                <c:choose>
                    <c:when test="${not empty currentTeacher and not empty currentTeacher.user and not empty currentTeacher.user.realName}">
                        ${currentTeacher.user.realName}
                    </c:when>
                    <c:when test="${not empty sessionScope.loggedInUser and not empty sessionScope.loggedInUser.realName}">
                        ${sessionScope.loggedInUser.realName}
                    </c:when>
                    <c:when test="${not empty sessionScope.loggedInUser and not empty sessionScope.loggedInUser.username}">
                        ${sessionScope.loggedInUser.username}
                    </c:when>
                    <c:otherwise>
                        教学管理员
                    </c:otherwise>
                </c:choose>
            </span>
        <div class="user-dropdown" id="userDropdown">
            <a href="#" class="user-dropdown-item" onclick="showProfileModal()">
                <i class="bi bi-person-circle"></i>
                个人资料
            </a>
            <a href="${pageContext.request.contextPath}/teacher/changePassword" class="user-dropdown-item">
                <i class="bi bi-key"></i>
                修改密码
            </a>
            <a href="${pageContext.request.contextPath}/user/logout" class="user-dropdown-item">
                <i class="bi bi-box-arrow-right"></i>
                退出登录
            </a>
        </div>
    </div>
</div>

<!-- 侧边栏 -->
<div class="sidebar">
    <ul class="sidebar-menu">
        <li>
            <a href="${pageContext.request.contextPath}/teacher/home">
                <i class="bi bi-house-door"></i> 教师首页
            </a>
        </li>
        <li>
            <a href="#" class="has-submenu">
                <i class="bi bi-megaphone"></i> 通知管理
            </a>
            <ul class="sidebar-submenu">
                <li><a href="${pageContext.request.contextPath}/teacher/notifications/academic">教学通知</a></li>
                <li><a href="${pageContext.request.contextPath}/teacher/notifications/exam">考试安排</a></li>
                <li><a href="${pageContext.request.contextPath}/teacher/notifications/classroom">教学安排</a></li>
            </ul>
        </li>
        <li class="open">
            <a href="#" class="has-submenu">
                <i class="bi bi-book"></i> 教学管理
            </a>
            <ul class="sidebar-submenu">
                <li><a href="${pageContext.request.contextPath}/teacher/courses" class="active">我的课程</a></li>
                <li><a href="${pageContext.request.contextPath}/teacher/course-students">选课学生</a></li>
                <li><a href="${pageContext.request.contextPath}/teacher/homework">作业管理</a></li>
                <li><a href="${pageContext.request.contextPath}/teacher/grades">成绩管理</a></li>
                <li><a href="${pageContext.request.contextPath}/teacher/attendance">考勤管理</a></li>
            </ul>
        </li>
        <li>
            <a href="#" class="has-submenu">
                <i class="bi bi-person-check"></i> 学生管理
            </a>
            <ul class="sidebar-submenu">
                <li><a href="${pageContext.request.contextPath}/teacher/evaluation">教学评价</a></li>
                <li><a href="${pageContext.request.contextPath}/teacher/leave">请假申请</a></li>
            </ul>
        </li>
        <li>
            <a href="${pageContext.request.contextPath}/user/logout">
                <i class="bi bi-box-arrow-right"></i> 退出登录
            </a>
        </li>
    </ul>
</div>

<!-- 内容区 -->
<div class="content">
    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
        <h1 class="h2">我的课程</h1>
    </div>

    <c:if test="${not empty error}">
        <div class="alert alert-danger" role="alert">
                ${error}
        </div>
    </c:if>

    <c:if test="${not empty param.error}">
        <div class="alert alert-danger" role="alert">
            请求失败，请检查网络或联系管理员。
        </div>
    </c:if>

    <div class="card">
        <div class="card-body">
            <!-- 查询表单 -->
            <div class="d-flex justify-content-between align-items-center mb-3">
                <form class="search-bar" method="get" action="${pageContext.request.contextPath}/teacher/courses" style="margin-bottom: 0;">
                    <input type="text" class="form-control" name="courseSearch" value="${courseSearch}" placeholder="请输入课程名称">
                    <button type="submit" class="btn btn-primary">查询</button>
                    <a href="${pageContext.request.contextPath}/teacher/courses" class="btn btn-warning">重置</a>
                </form>

                <button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#addCourseModal">
                    <i class="bi bi-plus-circle"></i> 添加课程
                </button>
            </div>

            <!-- 课程列表 -->
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                    <tr>
                        <th style="width: 8%;">序号</th>
                        <th style="width: 25%;">课程名称</th>
                        <th style="width: 8%;">学分</th>
                        <th style="width: 10%;">最大人数</th>
                        <th style="width: 15%;">教学时间</th>
                        <th style="width: 12%;">教学地点</th>
                        <th style="width: 10%;">状态</th>
                        <th style="width: 12%;">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <c:if test="${empty courses}">
                        <tr>
                            <td colspan="8" class="text-center">暂无课程信息</td>
                        </tr>
                    </c:if>
                    <c:if test="${not empty courses}">
                        <c:forEach var="course" items="${courses}" varStatus="status">
                            <tr>
                                <td>${status.count + (currentPage - 1) * pageSize}</td>
                                <td style="text-align: left;">
                                    <c:out value="${course.name}"/>
                                </td>
                                <td>${course.credits}</td>
                                <td>${course.maxStudents}</td>
                                <td>
                                    <c:choose>
                                        <c:when test="${not empty course.courseScheduleList}">
                                            <c:forEach var="schedule" items="${course.courseScheduleList}" varStatus="scheduleStatus">
                                                <div class="schedule-item" style="margin-bottom: 2px;">
                                                    <c:choose>
                                                        <c:when test="${schedule.weekday == 1}">周一</c:when>
                                                        <c:when test="${schedule.weekday == 2}">周二</c:when>
                                                        <c:when test="${schedule.weekday == 3}">周三</c:when>
                                                        <c:when test="${schedule.weekday == 4}">周四</c:when>
                                                        <c:when test="${schedule.weekday == 5}">周五</c:when>
                                                        <c:when test="${schedule.weekday == 6}">周六</c:when>
                                                        <c:when test="${schedule.weekday == 7}">周日</c:when>
                                                        <c:otherwise>${schedule.weekday}</c:otherwise>
                                                    </c:choose>
                                                    第${schedule.period}节
                                                </div>
                                            </c:forEach>
                                        </c:when>
                                        <c:otherwise>
                                            <span class="text-muted">暂无信息</span>
                                        </c:otherwise>
                                    </c:choose>
                                </td>
                                <td>
                                    <c:choose>
                                        <c:when test="${not empty course.courseScheduleList}">
                                            <c:forEach var="schedule" items="${course.courseScheduleList}" varStatus="scheduleStatus">
                                                <div class="classroom-item" style="margin-bottom: 2px;">
                                                    <c:choose>
                                                        <c:when test="${not empty schedule.classroom}">
                                                            <c:out value="${schedule.classroom}"/>
                                                        </c:when>
                                                        <c:otherwise>
                                                            <span class="text-muted">暂无</span>
                                                        </c:otherwise>
                                                    </c:choose>
                                                </div>
                                            </c:forEach>
                                        </c:when>
                                        <c:otherwise>
                                            <span class="text-muted">暂无信息</span>
                                        </c:otherwise>
                                    </c:choose>
                                </td>
                                <td>
                                    <c:choose>
                                        <c:when test="${course.status == 1}">
                                            <span class="badge bg-success">启用</span>
                                        </c:when>
                                        <c:otherwise>
                                            <span class="badge bg-secondary">禁用</span>
                                        </c:otherwise>
                                    </c:choose>
                                </td>
                                <td>
                                    <div class="action-buttons">
                                        <button type="button" class="btn btn-primary btn-sm edit-btn" data-id="${course.id}">
                                            <i class="bi bi-pencil"></i> 编辑
                                        </button>
                                        <button type="button" class="btn btn-danger btn-sm delete-btn" data-id="${course.id}">
                                            <i class="bi bi-trash"></i> 删除
                                        </button>
                                    </div>
                                </td>
                            </tr>
                        </c:forEach>
                    </c:if>
                    </tbody>
                </table>
            </div>

            <!-- 分页 -->
            <c:if test="${not empty courses}">
                <nav aria-label="Page navigation">
                    <c:if test="${totalPages > 1}">
                        <ul class="pagination justify-content-center">
                            <c:if test="${currentPage > 1}">
                                <li class="page-item">
                                    <a class="page-link" href="?page=${currentPage - 1}&courseSearch=${courseSearch}" aria-label="Previous">
                                        <span aria-hidden="true">&laquo;</span>
                                    </a>
                                </li>
                            </c:if>

                            <c:forEach begin="1" end="${totalPages}" var="i">
                                <li class="page-item ${currentPage == i ? 'active' : ''}">
                                    <a class="page-link" href="?page=${i}&courseSearch=${courseSearch}">${i}</a>
                                </li>
                            </c:forEach>

                            <c:if test="${currentPage < totalPages}">
                                <li class="page-item">
                                    <a class="page-link" href="?page=${currentPage + 1}&courseSearch=${courseSearch}" aria-label="Next">
                                        <span aria-hidden="true">&raquo;</span>
                                    </a>
                                </li>
                            </c:if>
                        </ul>
                    </c:if>
                    <div class="text-center mt-2" style="font-size:0.98rem;color:#888;">
                        共${totalRecords}条记录，当前第${currentPage}/${totalPages}页
                    </div>
                </nav>
            </c:if>
        </div>
    </div>
</div>

<!-- 添加/编辑课程模态框 -->
<div class="modal fade" id="addCourseModal" tabindex="-1" aria-labelledby="addCourseModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="addCourseModalLabel">添加课程</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="addCourseForm" method="post" action="${pageContext.request.contextPath}/teacher/courses/add">
                <div class="modal-body">
                    <input type="hidden" id="courseId" name="id">

                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group mb-3">
                                <label for="courseName" class="form-label">课程名称 <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="courseName" name="name" required maxlength="100">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group mb-3">
                                <label for="credits" class="form-label">学分 <span class="text-danger">*</span></label>
                                <input type="number" class="form-control" id="credits" name="credits" required min="1" max="10">
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group mb-3">
                                <label for="maxStudents" class="form-label">最大人数 <span class="text-danger">*</span></label>
                                <input type="number" class="form-control" id="maxStudents" name="maxStudents" required min="1" max="200">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group mb-3">
                                <label for="status" class="form-label">课程状态</label>
                                <select class="form-control" id="status" name="status">
                                    <option value="1">启用</option>
                                    <option value="0">禁用</option>
                                </select>
                            </div>
                        </div>
                    </div>

                    <!-- 课程时间安排 -->
                    <div class="row">
                        <div class="col-12">
                            <label class="form-label">课程时间安排</label>
                            <div id="scheduleContainer">
                                <div class="schedule-row row mb-2">
                                    <div class="col-md-4">
                                        <select class="form-control weekday-select" name="weekdays[]">
                                            option value="">选择周几</option>
                                            <option value="1">周一</option>
                                            <option value="2">周二</option>
                                            <option value="3">周三</option>
                                            <option value="4">周四</option>
                                            <option value="5">周五</option>
                                            <option value="6">周六</option>
                                            <option value="7">周日</option>
                                        </select>
                                    </div>
                                    <div class="col-md-3">
                                        <select class="form-control period-select" name="periods[]">
                                            <option value="">选择节次</option>
                                            <option value="1">第1节</option>
                                            <option value="2">第2节</option>
                                            <option value="3">第3节</option>
                                            <option value="4">第4节</option>
                                            <option value="5">第5节</option>
                                            <option value="6">第6节</option>
                                            <option value="7">第7节</option>
                                            <option value="8">第8节</option>
                                            <option value="9">第9节</option>
                                            <option value="10">第10节</option>
                                        </select>
                                    </div>
                                    <div class="col-md-4">
                                        <input type="text" class="form-control classroom-input" name="classrooms[]" placeholder="教学地点 (如 3A202)">
                                    </div>
                                    <div class="col-md-1">
                                        <button type="button" class="btn btn-danger btn-sm remove-schedule" style="display: none;">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </div>
                                </div>
                            </div>
                            <button type="button" class="btn btn-sm btn-success" id="addScheduleBtn">
                                <i class="bi bi-plus"></i> 添加时间安排
                            </button>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="${pageContext.request.contextPath}/resources/bootstrap5/js/bootstrap.bundle.min.js"></script>
<script>
    // 用户名下拉菜单
    document.getElementById('userDropdownContainer').addEventListener('click', function(e) {
        e.stopPropagation();
        const dropdown = document.getElementById('userDropdown');
        dropdown.classList.toggle('show');
    });

    // 点击页面其他地方关闭下拉菜单
    document.addEventListener('click', function() {
        const dropdown = document.getElementById('userDropdown');
        dropdown.classList.remove('show');
    });

    // 侧边栏子菜单切换
    function toggleSubmenu(element) {
        const parent = element.parentElement;
        const submenu = parent.querySelector('.sidebar-submenu');

        // 关闭其他子菜单
        document.querySelectorAll('.sidebar-menu > li').forEach(li => {
            if (li !== parent) {
                li.classList.remove('open');
            }
        });

        // 切换当前子菜单
        parent.classList.toggle('open');
    }

    // 页面加载时初始化
    document.addEventListener('DOMContentLoaded', function() {
        // 初始化侧边栏子菜单点击事件
        document.querySelectorAll('.has-submenu').forEach(item => {
            item.addEventListener('click', function(e) {
                e.preventDefault();
                toggleSubmenu(this);
            });
        });

        // 初始化添加课程模态框
        const addCourseModal = document.getElementById('addCourseModal');
        if (addCourseModal) {
            addCourseModal.addEventListener('hidden.bs.modal', function() {
                document.getElementById("addCourseModalLabel").textContent = "添加课程";
                document.getElementById("addCourseForm").reset();
                document.getElementById("courseId").value = "";
                document.getElementById("addCourseForm").setAttribute("action", "${pageContext.request.contextPath}/teacher/courses/add");

                // 重置时间安排
                resetScheduleRows();
            });
        }

        // 添加时间安排
        const addScheduleBtn = document.getElementById('addScheduleBtn');
        if (addScheduleBtn) {
            addScheduleBtn.addEventListener('click', function() {
                addScheduleRow();
            });
        }

        // 删除时间安排
        document.addEventListener('click', function(e) {
            if (e.target.closest('.remove-schedule')) {
                e.target.closest('.schedule-row').remove();
                updateRemoveButtons();
            }
        });

        // 编辑课程
        document.querySelectorAll('.edit-btn').forEach(btn => {
            btn.addEventListener('click', function() {
                const id = this.getAttribute('data-id');
                fetch("${pageContext.request.contextPath}/teacher/courses/view/" + id)
                    .then(response => response.json())
                    .then(data => {
                        if(data.success) {
                            const course = data.course;
                            document.getElementById("addCourseModalLabel").textContent = "编辑课程";
                            document.getElementById("courseName").value = course.name;
                            document.getElementById("credits").value = course.credits;
                            document.getElementById("maxStudents").value = course.maxStudents;
                            document.getElementById("status").value = course.status;
                            document.getElementById("courseId").value = course.id;
                            document.getElementById("addCourseForm").setAttribute("action", "${pageContext.request.contextPath}/teacher/courses/edit");


                            // 初始化时间安排
                            resetScheduleRows();
                            if(course.courseScheduleList && course.courseScheduleList.length > 0) {
                                course.courseScheduleList.forEach(schedule => {
                                    addScheduleRow(schedule.weekday, schedule.period, schedule.classroom);
                                });
                            }

                            const modal = new bootstrap.Modal(document.getElementById('addCourseModal'));
                            modal.show();
                        } else {
                            alert("获取课程信息失败：" + data.message);
                        }
                    })
                    .catch(error => {
                        console.error('Error:', error);
                        alert("获取课程信息失败，请稍后重试");
                    });
            });
        });

        // 删除课程
        document.querySelectorAll('.delete-btn').forEach(btn => {
            btn.addEventListener('click', function() {
                const id = this.getAttribute('data-id');
                if(confirm("确定要删除该课程吗？删除后将无法恢复！")) {
                    window.location.href = "${pageContext.request.contextPath}/teacher/courses/delete/" + id;
                }
            });
        });
    });

    // 添加时间安排行
    function addScheduleRow(weekday, period, classroom) {
        const scheduleRowHtml = `
                <div class="schedule-row row mb-2">
                    <div class="col-md-4">
                        <select class="form-control weekday-select" name="weekdays[]">
                            <option value="">选择周几</option>
                            <option value="1" ${weekday == 1 ? 'selected' : ''}>周一</option>
                            <option value="2" ${weekday == 2 ? 'selected' : ''}>周二</option>
                            <option value="3" ${weekday == 3 ? 'selected' : ''}>周三</option>
                            <option value="4" ${weekday == 4 ? 'selected' : ''}>周四</option>
                            <option value="5" ${weekday == 5 ? 'selected' : ''}>周五</option>
                            <option value="6" ${weekday == 6 ? 'selected' : ''}>周六</option>
                            <option value="7" ${weekday == 7 ? 'selected' : ''}>周日</option>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <select class="form-control period-select" name="periods[]">
                            <option value="">选择节次</option>
                            <option value="1" ${period == 1 ? 'selected' : ''}>第1节</option>
                            <option value="2" ${period == 2 ? 'selected' : ''}>第2节</option>
                            <option value="3" ${period == 3 ? 'selected' : ''}>第3节</option>
                            <option value="4" ${period == 4 ? 'selected' : ''}>第4节</option>
                            <option value="5" ${period == 5 ? 'selected' : ''}>第5节</option>
                            <option value="6" ${period == 6 ? 'selected' : ''}>第6节</option>
                            <option value="7" ${period == 7 ? 'selected' : ''}>第7节</option>
                            <option value="8" ${period == 8 ? 'selected' : ''}>第8节</option>
                            <option value="9" ${period == 9 ? 'selected' : ''}>第9节</option>
                            <option value="10" ${period == 10 ? 'selected' : ''}>第10节</option>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <input type="text" class="form-control classroom-input" name="classrooms[]" placeholder="教学地点 (如 3A202)" value="${classroom ? classroom : ''}">
                    </div>
                    <div class="col-md-1">
                        <button type="button" class="btn btn-danger btn-sm remove-schedule">
                            <i class="bi bi-trash"></i>
                        </button>
                    </div>
                </div>
            `;
        document.getElementById('scheduleContainer').insertAdjacentHTML('beforeend', scheduleRowHtml);
        updateRemoveButtons();
    }

    // 重置时间安排
    function resetScheduleRows() {
        const scheduleContainer = document.getElementById('scheduleContainer');
        if (scheduleContainer) {
            scheduleContainer.innerHTML = `
                    <div class="schedule-row row mb-2">
                        <div class="col-md-4">
                            <select class="form-control weekday-select" name="weekdays[]">
                                <option value="">选择周几</option>
                                <option value="1">周一</option>
                                <option value="2">周二</option>
                                <option value="3">周三</option>
                                <option value="4">周四</option>
                                <option value="5">周五</option>
                                <option value="6">周六</option>
                                <option value="7">周日</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <select class="form-control period-select" name="periods[]">
                                <option value="">选择节次</option>
                                <option value="1">第1节</option>
                                <option value="2">第2节</option>
                                <option value="3">第3节</option>
                                <option value="4">第4节</option>
                                <option value="5">第5节</option>
                                <option value="6">第6节</option>
                                <option value="7">第7节</option>
                                <option value="8">第8节</option>
                                <option value="9">第9节</option>
                                <option value="10">第10节</option>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <input type="text" class="form-control classroom-input" name="classrooms[]" placeholder="教学地点 (如 3A202)">
                        </div>
                        <div class="col-md-1">
                            <button type="button" class="btn btn-danger btn-sm remove-schedule" style="display: none;">
                                <i class="bi bi-trash"></i>
                            </button>
                        </div>
                    </div>
                `;
        }
    }

    // 更新删除按钮状态
    function updateRemoveButtons() {
        const scheduleRows = document.querySelectorAll('.schedule-row');
        const removeButtons = document.querySelectorAll('.remove-schedule');

        if (scheduleRows.length > 1) {
            removeButtons.forEach(btn => btn.style.display = 'block');
        } else {
            removeButtons.forEach(btn => btn.style.display = 'none');
        }
    }
</script>
</body>
</html>

